﻿#include "MdbStructs.h"
#include "MemCacheTemplateSingleton.h"
#include <string>
#include <cstring>

namespace mdb
{
	thread_local char t_MdbDataStringBuffer[10240];

	Tick* Tick::Allocate()
	{
		return ::Allocate<Tick>();
	}
	void Tick::Free()
	{
		::Free<Tick>(this);
	}
	const char* Tick::GetString() const
	{
		sprintf(t_MdbDataStringBuffer, "%lld,%s,%s,%lld,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f",
			TradingDay, ExchangeID, InstrumentID, UpdateTs, LastPrice, LastTraded, Volume, OpenInterest, Turnover, BidPrice1, BidVolume1, AskPrice1, AskVolume1, PreSettlementPrice, PreClosePrice, PreOpenInterest, HighestPrice, LowestPrice);
		return t_MdbDataStringBuffer;
	}
	int Tick::GetSqlString(char* buff) const
	{
		return sprintf(buff, "\n('%lld','%s','%s','%lld','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f'),",
			TradingDay, ExchangeID, InstrumentID, UpdateTs, LastPrice, LastTraded, Volume, OpenInterest, Turnover, BidPrice1, BidVolume1, AskPrice1, AskVolume1, PreSettlementPrice, PreClosePrice, PreOpenInterest, HighestPrice, LowestPrice);
	}
	const char* Tick::GetDebugString() const
	{
		sprintf(t_MdbDataStringBuffer, "Tick:TradingDay:[%lld], ExchangeID:[%s], InstrumentID:[%s], UpdateTs:[%lld], LastPrice:[%f], LastTraded:[%f], Volume:[%f], OpenInterest:[%f], Turnover:[%f], BidPrice1:[%f], BidVolume1:[%f], AskPrice1:[%f], AskVolume1:[%f], PreSettlementPrice:[%f], PreClosePrice:[%f], PreOpenInterest:[%f], HighestPrice:[%f], LowestPrice:[%f]",
			TradingDay, ExchangeID, InstrumentID, UpdateTs, LastPrice, LastTraded, Volume, OpenInterest, Turnover, BidPrice1, BidVolume1, AskPrice1, AskVolume1, PreSettlementPrice, PreClosePrice, PreOpenInterest, HighestPrice, LowestPrice);
		return t_MdbDataStringBuffer;
	}

	Bar* Bar::Allocate()
	{
		return ::Allocate<Bar>();
	}
	void Bar::Free()
	{
		::Free<Bar>(this);
	}
	const char* Bar::GetString() const
	{
		sprintf(t_MdbDataStringBuffer, "%lld,%s,%s,%lld,%s,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%d,%s",
			TradingDay, ExchangeID, InstrumentID, UpdateTs, Prece, LastTraded, Volume, Turnover, OpenInterest, Open, High, Low, Close, HighestPrice, LowestPrice, (int)ProductClass, UnderlyingID);
		return t_MdbDataStringBuffer;
	}
	int Bar::GetSqlString(char* buff) const
	{
		return sprintf(buff, "\n('%lld','%s','%s','%lld','%s','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%d','%s'),",
			TradingDay, ExchangeID, InstrumentID, UpdateTs, Prece, LastTraded, Volume, Turnover, OpenInterest, Open, High, Low, Close, HighestPrice, LowestPrice, (int)ProductClass, UnderlyingID);
	}
	const char* Bar::GetDebugString() const
	{
		sprintf(t_MdbDataStringBuffer, "Bar:TradingDay:[%lld], ExchangeID:[%s], InstrumentID:[%s], UpdateTs:[%lld], Prece:[%s], LastTraded:[%f], Volume:[%f], Turnover:[%f], OpenInterest:[%f], Open:[%f], High:[%f], Low:[%f], Close:[%f], HighestPrice:[%f], LowestPrice:[%f], ProductClass:[%d], UnderlyingID:[%s]",
			TradingDay, ExchangeID, InstrumentID, UpdateTs, Prece, LastTraded, Volume, Turnover, OpenInterest, Open, High, Low, Close, HighestPrice, LowestPrice, (int)ProductClass, UnderlyingID);
		return t_MdbDataStringBuffer;
	}


	thread_local Tick t_CompareTick;
	thread_local Bar t_CompareBar;

}
